﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;

namespace LIB
{
    public class App_Mail
    {
        public static void Send_Email(string SendFrom, string SendTo, string CC, string Subject, string Body, string mailSend, string passSend)
        {
            try
            {
                //Create Mail Message Object with content that you want to send with mail.
                System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(SendFrom, SendTo, Subject, Body);

                MyMailMessage.IsBodyHtml = true;
                string[] arrCC = CC.Split('-');
                for (int i = 0; i < arrCC.Length - 1; i++)
                    MyMailMessage.Bcc.Add(arrCC[i]);

                //Proper Authentication Details need to be passed when sending email from gmail
                System.Net.NetworkCredential mailAuthentication = new
                System.Net.NetworkCredential(mailSend, passSend);

                //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
                //For different server like yahoo this details changes and you can
                //get it from respective server.
                System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);

                //Enable SSL
                mailClient.EnableSsl = true;

                mailClient.UseDefaultCredentials = false;

                mailClient.Credentials = mailAuthentication;

                mailClient.Send(MyMailMessage);
            }

            catch (Exception ex) { }
        }
        public static void Send_Email(string SendFrom, string SendTo, string CC, string Subject, string Body)
        {
            try
            {
                //Create Mail Message Object with content that you want to send with mail.
                System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(SendFrom, SendTo,Subject, Body);

                MyMailMessage.IsBodyHtml = true;
                string[] arrCC = CC.Split('-');
                for (int i = 0; i < arrCC.Length - 1; i++)
                    MyMailMessage.CC.Add(arrCC[i]);

                //Proper Authentication Details need to be passed when sending email from gmail
                System.Net.NetworkCredential mailAuthentication = new
                System.Net.NetworkCredential("unreply.notification@gmail.com", "d0983034303");

                //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
                //For different server like yahoo this details changes and you can
                //get it from respective server.
                System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);

                //Enable SSL
                mailClient.EnableSsl = true;

                mailClient.UseDefaultCredentials = false;

                mailClient.Credentials = mailAuthentication;

                mailClient.Send(MyMailMessage);
            }

            catch (Exception ex) { }
        }
        public static void SendToFriend(string SendFrom, string SendTo,string Subject, string Body)
        {
            try
            {
                //Create Mail Message Object with content that you want to send with mail.
                System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(SendFrom, SendTo,Subject, Body);

                MyMailMessage.IsBodyHtml = true;
              
                //Proper Authentication Details need to be passed when sending email from gmail
                System.Net.NetworkCredential mailAuthentication = new
                System.Net.NetworkCredential("unreply.notification@gmail.com", "d0983034303");

                //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
                //For different server like yahoo this details changes and you can
                //get it from respective server.
                System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);

                //Enable SSL
                mailClient.EnableSsl = true;

                mailClient.UseDefaultCredentials = false;

                mailClient.Credentials = mailAuthentication;

                mailClient.Send(MyMailMessage);
            }

            catch (Exception ex) { }
        }
        public static bool WebSend(string p_To, string p_Subject, string p_Body, string p_From)
        {
            bool success;

            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.gmail.com";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = "465";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "postmaster@tourismvietnam.com.vn";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "123456";

            mail.To = p_To;
            mail.Subject = p_Subject;
            mail.From = p_From;
            mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            mail.BodyEncoding = System.Text.ASCIIEncoding.UTF8;
            mail.Body = p_Body;

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";

            try
            {
                System.Web.Mail.SmtpMail.Send(mail);
                success = true;
            }
            catch
            {
                success = false;
            }

            return success;
        }
        public static bool WebSendInvi(string p_From, string p_To, string p_CC, string p_Subject, string p_Body)
        {
            bool success;

            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.gmail.com";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = "465";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "postmaster@tourismvietnam.com.vn";
            mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "123456";

            mail.To = p_To;
            mail.Cc = p_CC.Replace(" ", ",");
            mail.Subject = p_Subject;
            mail.From = p_From;
            mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            mail.BodyEncoding = System.Text.ASCIIEncoding.UTF8;
            mail.Body = p_Body;

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";

            try
            {
                System.Web.Mail.SmtpMail.Send(mail);
                success = true;
            }
            catch(Exception ex)
            {
                success = false;
            }

            return success;
        }
    }
}
